#include <iostream>
#include "LinkedList.h"
using namespace std;

//  99.41 %
class Solution1
{
  public:
	ListNode *removeElements(ListNode *head, int val)
	{
		while (head && head->val == val)
		{
			ListNode *delNode = head;
			head = head->next;
			delete delNode;
		}
		if (head == NULL)
			return NULL;
		ListNode *cur = head;
		while (cur->next != NULL)
		{
			if (cur->next->val == val)
			{
				ListNode *delNode = cur->next;
				cur->next = delNode->next;
				delete delNode;
			}
			else
			{
				cur = cur->next;
			}
		}
		return head;
	}
};
// 96.93 %
class Solution
{
  public:
	ListNode *removeElements(ListNode *head, int val)
	{
		ListNode *dummyHead = new ListNode(0);
		dummyHead->next = head;
		ListNode *cur = dummyHead;
		while (cur->next != NULL)
		{
			if (cur->next->val == val)
			{
				ListNode *delNode = cur->next;
				cur->next = delNode->next;
				delete delNode;
			}
			else
			{
				cur = cur->next;
			}
		}
		ListNode *retNode = dummyHead->next;
		delete dummyHead;
		return retNode;
	}
};

int main(int argc, char const *argv[])
{
	int arr[] = {6, 6, 6, 6, 6, 6};
	int n = sizeof(arr) / sizeof(arr[0]);
	ListNode *list = createLinkedList(arr, n);
	int x = 6;
	printLinkedList(list);
	list = Solution().removeElements(list, x);
	printLinkedList(list);
	deleteLinkedList(list);
	return 0;
}
